package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"smart-planet/cache"
	"smart-planet/models"
	"time"
)

func CollList(c *gin.Context) {
	var coll []models.Coll
	redis_key := "coll_list"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Find(&coll)
		jsonData, _ := json.Marshal(coll)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &coll)
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "列表展示成功",
		"data": coll,
	})
	return
}
